import PageConst from './PageConst';

export default {

  defaults(props) {
    return {
      ...PageConst,
    }
  },

  async queryWeatherInfo(ctx) {
    const content = await ctx.fn.DB.ycjk.queryWeatherInfo({Authorization: ctx.getProps().token}).catch(
      error => {
        console.log(error);
        return false;
      }
    );
    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          weather: {$set: content}
        })
      );
    }
  },

  async alertHistoryLastDay(ctx, props) {
    const content = await ctx.fn.DB.ycjk.alertHistoryLastDay2({Authorization: ctx.getProps().token, day: props.day}).catch(
      error => {
        console.log(error);
        return false;
      }
    );

    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          alertHistoryLastDay: {
            series: {
              $apply: (series) => {
                return series.map(item => {
                  if (item.name === '黄色预警') {
                    return React.addons.update(item, { data: {$set: content.yellow}});
                  } else if (item.name === '红色预警') {
                    return React.addons.update(item, { data: {$set: content.red}});
                  } else {
                    return item;
                  }
                })
              }
            },
            xAxis: {
              data: {$set: content.days}
            }
          }
        })
      );
    }
  },

  async alertCountRank(ctx) {
    const content = await ctx.fn.DB.ycjk.alertCountRank({Authorization: ctx.getProps().token}).catch(
      error => {
        console.log(error);
        return false;
      }
    );

    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          alertCountRank: {
            jsxdata: {
              data: {$set: content}
            }
          },
        })
      );
    }
  },

  async todayWorkInfoByEcode(ctx, props){
    const content = await ctx.fn.DB.ycjk.todayWorkInfoByEcode({Authorization: ctx.getProps().token, ...props}).catch(
      error => {
      console.log(error);
      return false;
      }
    );

    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          alertDetails: {
            data: {$set: content}
          }
        })
      );
    }
  },

  async topAlertCount(ctx) {
    const content = await ctx.fn.DB.ycjk.topAlertCount({Authorization: ctx.getProps().token}).catch(
      error => {
        console.log(error);
        return false;
      }
    );

    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          engCount: {$set: content.projectsSum},
          equipmentCount: {$set: content.equipSum},
          redAlertCount: {$set: content.redAlert},
          yellowAlertCount: {$set: content.yellowAlert},
        })
      );
    }
  },

  async engCount(ctx) {
    const content = await ctx.fn.DB.ycjk.engCount({Authorization: ctx.getProps().token}).catch(
      error => {
        console.log(error);
        return false;
      }
    );

    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          engCount: {$set: content.v}
        })
      );
    }
  },

  async equipmentCount(ctx) {
    const content = await ctx.fn.DB.ycjk.equipmentCount({Authorization: ctx.getProps().token}).catch(
      error => {
        console.log(error);
        return false;
      }
    );

    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          equipmentCount: {$set: content.v}
        })
      );
    }
  },

  async yellowAlertCount(ctx) {
    const content = await ctx.fn.DB.ycjk.yellowAlertCount({Authorization: ctx.getProps().token}).catch(
      error => {
        console.log(error);
        return false;
      }
    );

    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          yellowAlertCount: {$set: content.v}
        })
      );
    }
  },

  async redAlertCount(ctx) {
    const content = await ctx.fn.DB.ycjk.redAlertCount({Authorization: ctx.getProps().token}).catch(
      error => {
        console.log(error);
        return false;
      }
    );

    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          redAlertCount: {$set: content.v}
        })
      );
    }
  },

  async allEquipmentInfo(ctx, props) {
    const content = await ctx.fn.DB.ycjk.allEquipmentInfo({Authorization: ctx.getProps().token, ...props}).catch(
      error => {
        console.log(error);
        return false;
      }
    );

    if (content) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          equipmentAllInfo: {
            jsxdata: {$set: content},
            search: {$set: props}
          }
        })
      );
    } else {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          equipmentAllInfo: {
            jsxdata: {
              data: {$set: []},
              currentPage: {$set: 1},
              totalCount: {$set: 0}
            },
            search: {$set: props}
          }
        })
      );
    }
  }
}
